package com.controller.backend;

import com.biz.ExamineeHallBiz;
import com.biz.OperationLogBiz;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.ExamineeHall;
import com.entity.model.ManagerInfo;
import com.entity.model.OperationLog;
import com.util.SystemConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jc on 2023/03/15.
 */
@Controller
@RequestMapping("/backend/security/examineeHall")
public class ExamineeHallController extends BaseController<ExamineeHall> {
    @Autowired
    private ExamineeHallBiz examineeHallBiz;

    @Autowired
    private OperationLogBiz operationLogBiz;

    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/examineeHall/list";
    }

    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/examineeHall/form";
    }

    @RequestMapping(value = "addExamineeHall")
    @ResponseBody
    public Result addExamineeHall(HttpSession session, ExamineeHall examineeHall) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (examineeHall != null) {
            baseBiz.save(examineeHall);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(3);
            operationLog.setLogRemark("新增考场");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(examineeHall);
        }
        return ResultGenerator.genFailResult("操作失败");
    }

    /**
     * 更新一个对象
     */
    @RequestMapping(value = "updateExamineeHall")
    @ResponseBody
    public Result updateExamineeHall(HttpSession session, ExamineeHall examineeHall) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (examineeHall != null) {
            baseBiz.update(examineeHall);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(4);
            operationLog.setLogRemark("修改考场信息");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(examineeHall);
        }
        return ResultGenerator.genFailResult("操作失败");
    }


    @RequestMapping(value = "/getAllHall")
    @ResponseBody
    public Result getAllHall() {
        Condition condition = new Condition(ExamineeHall.class);
        condition.createCriteria().andEqualTo("status", 0);
        List<ExamineeHall> examineeHalls = examineeHallBiz.findByCondition(condition);
        return ResultGenerator.genSuccessResult(examineeHalls, examineeHalls.size());
    }
}
